package com.xmb.wechat.delegate;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xmb.wechat.R;
import com.xmb.wechat.baserecyclerview.BaseRecyclerViewManager;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.entity.GroupCallEntity;

import java.util.ArrayList;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/29 14:33
 */
public class GroupCallDelegate extends BaseRecyclerViewManager {

    public GroupCallDelegate(Activity activity) {
        super(activity);
    }

    @Override
    public BaseQuickAdapter initAdapter() {
        return new BaseQuickAdapter<GroupCallEntity, BaseViewHolder>(R.layout.item_group_call, new ArrayList<>()) {
            @Override
            protected void convert(BaseViewHolder helper, GroupCallEntity item) {
                ImageView imageView = helper.getView(R.id.iv_avatar);
                WechatContactBean.setupAvatarIntoImageView(item.getAvatarSrc(), "", item.getAvatar(), imageView, 12);
                helper.setText(R.id.tv_avatar, TextUtils.isEmpty(item.getName()) ? "" : item.getName());
                switch (item.getState()) {
                    case 0:
                        helper.setText(R.id.tv_state, "(连接中)");
                        helper.setTextColor(R.id.tv_state, context.getResources().getColor(R.color.red_70));
                        break;
                    case 1:
                        helper.setText(R.id.tv_state, "(正常)");
                        helper.setTextColor(R.id.tv_state, context.getResources().getColor(R.color.green));
                        break;
                    case 2:
                        helper.setText(R.id.tv_state, "(聊天中)");
                        helper.setTextColor(R.id.tv_state, context.getResources().getColor(R.color.blue_2));
                        break;
                }
                helper.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        new AlertDialog.Builder(context).setItems(new String[]{"删除"}, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mData.remove(item);
                                notifyDataSetChanged();
                            }
                        }).setNegativeButton("取消", null).show();
                        return false;
                    }
                });
            }
        };
    }

    @Override
    protected RecyclerView.LayoutManager getLayoutManager() {
        return new GridLayoutManager(context, 4);
    }
}
